package com.voole.dao;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import com.voole.entity.SysAuthority;
import com.voole.entity.SysCatalog;
import com.voole.entity.SysResource;

@Repository
public interface SysResourceDao {

	@Select(value="SELECT * FROM sys_resource ORDER BY sort")
	List<SysResource> getList();
	
	@Select(value="SELECT * FROM sys_resource WHERE catalogid = #{param} ORDER BY sort")
	List<SysResource> getListByCatalogId(@Param(value="param")Integer catalogId);
	
	@Select(value="SELECT DISTINCT(sr.id) FROM sys_action sa LEFT JOIN sys_resource sr ON sa.resourceid = sr.id WHERE sa.id IN (${actionIds})")
	List<Integer> getColumnResourceIdByActionId(@Param(value="actionIds")String actionIds);
	
	@Select(value="SELECT * FROM sys_resource WHERE id = ${param}")
	SysResource getSysResource(@Param(value="param")Integer resourceId);
	
	@Insert("INSERT INTO sys_resource (`resourcename`, `catalogid`, `sort`, `url`, `status`) VALUES(#{resourcename}, #{catalogid}, #{sort}, #{url}, #{status})")  
	@Options(useGeneratedKeys = true, keyProperty = "id")
	Integer addSysResource(SysResource sysCatalog);
	
	@Update("UPDATE sys_resource SET resourcename = #{resourcename}, catalogid = #{catalogid}, sort = #{sort}, url = #{url}, status = #{status} WHERE id = #{id} ")
	Integer updateSysResource(SysResource sysCatalog);
	
	@Delete(value="delete from sys_resource where id = #{resourceId}")
	Integer delSysResource(Integer resourceId);

	@Delete(value="delete from sys_resource where catalogid = #{catalogId}")
	Integer delListByCatalogId(Integer catalogId);
	
	@Delete(value="DELETE FROM sys_resource WHERE id IN ( SELECT sysrids.id FROM (SELECT sr.id FROM sys_resource sr LEFT JOIN sys_catalog sc ON sr.catalogid = sc.id LEFT JOIN sys_authority sau ON sc.authorityid = sau.id WHERE sau.id = #{authorityId}) sysrids)")
	Integer delListByAuthorityId(Integer authorityId);
}

